<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class WastageService extends Service {

    public static function gradeHeader($params,$type){
        $header = array(
            'Level'   => array('text' => PubFunc::t('等级')),
        );
        
        $start_time = strtotime($params['time_start']);
        $end_time = strtotime($params['time_end']);

        $allUser = '';
        $PlayUsers = '';
        if($type == 1){
        	$PlayUsers = '角色数';
        	$allUser = '创角总人数';
        }else if($type == 2){
        	$PlayUsers = '新用户人数';
        	$allUser = '新创角总人数';
        }else if($type == 3){
        	$PlayUsers = '活跃用户人数';
        	$allUser = '登录总人数';
        }
        
        for ($i=$start_time;$i<=$end_time;$i=$i+(60*60*24)){
        	$d = date('Y-m-d',$i);
        	$header[$d] = array('text' => $d, 'include' => array(
        			'PlayUsers_'.$d  => array('text' => $PlayUsers, 'rule' => 'numeric'),
        			'allUser_'.$d => array('text' => $allUser, 'rule' => 'numeric'),
        			'ratio_'.$d      => array('text' => '占比', 'rule' => 'numeric'),
        	));
        	
        }


        return $header;
    }
    
    
    
    public static function gradeData($params,$type){
    	if (PubFunc::checkEmpty($params, array('g', 'server', 'time_start', 'time_end'))) return array();
    
    	$modGameLogin = new ModGameLogin();
    
    	$data = $modGameLogin->getlevelData($params,$type);
    	$userData = $modGameLogin->getUserData($params,$type);
    	 
    	$new_userData = array();
    	foreach ($userData as $key=>$val){
    		$new_userData[$val['recdate']] = $val['allUser'];
    	}
    	
    	
    
    	$new_data = array();
    	$new_data2 = array();
    	foreach ($data as $key=>$val){
    		$new_data[$val['Level']]['Level'][] = $val['Level'];
    		$new_data[$val['Level']]['recdate'][] = $val['recdate'];
    		$new_data[$val['Level']]['PlayUsers'][] = $val['PlayUsers'];
    	}
    
    
    	foreach ($new_data as $key1=>$val1){
    		$new_data1 = array();
    		for($i=0;$i<count($val1['Level']);$i++){
    			$new_data1['Level'] = $key1;
    			$new_data1['PlayUsers_'.$val1['recdate'][$i]] = $val1['PlayUsers'][$i];
    		}
    		array_push($new_data2, $new_data1);
    	}
    
    
    	foreach ($new_data2 as $key => $row){
    		$level[$key]  = $row['Level'];
    	}
    	array_multisort($level, SORT_ASC ,$new_data2);
    
    
    	$new_data3 = array();
    	$start_time = strtotime($params['time_start']);
    	$end_time = strtotime($params['time_end']);
    	foreach ($new_data2 as $key => $row){
    		for ($j=$start_time;$j<=$end_time;$j=$j+(60*60*24)){
    			$d = date('Y-m-d',$j);
    		    if(!isset($row['PlayUsers_'.$d])){
    		    	$row['PlayUsers_'.$d] = 0;
    			}
    
    			if(isset($new_userData[$d])){
    				$row['allUser_'.$d] = $new_userData[$d];
    				$row['ratio_'.$d] = $row['PlayUsers_'.$d]? number_format($row['PlayUsers_'.$d] / $new_userData[$d] * 100, 2). '%' : 0;
    			}else{
    				$row['allUser_'.$d] = 0;
    				$row['ratio_'.$d] = 0;
    			}
    		}
    		$new_data3[$row['Level']] = $row;
    	}   	
    	
    	return $new_data3;
    	
    }

    

    
    
} 